#include "Map_Loader.h"

mapCell*** loadMapP(TiXmlNode* parent, MapInfo* info, mapCell*** &map) {
		
		if(!parent)
			return map;

		int t = parent->Type();
		int x, y, attr;

		TiXmlElement* element;
		TiXmlNode* child;

		switch(t) {

		case TiXmlNode::TINYXML_ELEMENT:
			if(strcmp(parent->Value(), "Tile") == 0) {
				element = parent->ToElement();
				element->Attribute("x", &x);
				element->Attribute("y", &y);
				element->Attribute("Walkable");

				if(strcmp(element->Attribute("Walkable"), "1") == 0)
					map[x][y]->walkable = true;
				else
					map[x][y]->walkable = false;

				if(strcmp(element->Attribute("team"), "1") == 0)
					map[x][y]->team = true;
				else
					map[x][y]->team = false;

				element->Attribute("Base", &attr);
				map[x][y]->base = attr;
				element->Attribute("Fringe", &attr);
				map[x][y]->fringe = attr;
				element->Attribute("Object", &attr);
				map[x][y]->object = attr;
				element->Attribute("Cost", &attr);
				map[x][y]->cost = attr;
				element->Attribute("Entity", &attr);
				map[x][y]->entity = attr;
			}
			else if(strcmp(parent->Value(), "Params") == 0) {
				element = parent->ToElement();
				element->Attribute("TileSize", &info->tileSize);
				element->Attribute("NumTilesX", &info->numTilesX);
				element->Attribute("NumTilesY", &info->numTilesY);

				map = new mapCell**[info->numTilesX];
				for(int i = 0; i < info->numTilesX; i++) {
					map[i] = new mapCell*[info->numTilesY];
					for(int j = 0; j < info->numTilesY; j++) {
						map[i][j] = new mapCell();
					}
				}
			}
		}

		for(child = parent->FirstChild(); child != 0; child = child->NextSibling())
			loadMapP(child, info, map);
		return map;
}

mapCell*** loadMap(MapInfo* info) {
	TiXmlDocument doc("level/LV1.xml");
	if(doc.LoadFile()) {
		mapCell*** map = NULL;
		return loadMapP(&doc, info, map);
	}
	else
		std::cout << "ERROR LOADING LEVEL";
	return NULL;
	
}

void saveMap(mapCell*** map, MapInfo* info) {
	TiXmlDocument doc;
	TiXmlDeclaration* decl;
	TiXmlElement* element;
	TiXmlElement* tile;


	decl = new TiXmlDeclaration("1.0", "", "");
	doc.LinkEndChild(decl);

	element = new TiXmlElement("Params");
	element->SetAttribute("TileSize", info->tileSize);
	element->SetAttribute("NumTilesX", info->numTilesX);
	element->SetAttribute("NumTilesY", info->numTilesY);
	doc.LinkEndChild(element);

	element = new TiXmlElement("Tiles");

	for(int i = 0; i < info->numTilesX; i++) {
		for(int j = 0; j < info->numTilesY; j++) {
			tile = new TiXmlElement("Tile");

			tile->SetAttribute("x", i);
			tile->SetAttribute("y", j); 
			tile->SetAttribute("Walkable", map[i][j]->walkable);
			tile->SetAttribute("Base", map[i][j]->base);
			tile->SetAttribute("Fringe", map[i][j]->fringe);
			tile->SetAttribute("Object", map[i][j]->object);
			tile->SetAttribute("Cost", map[i][j]->cost);
			tile->SetAttribute("Entity", map[i][j]->entity);
			tile->SetAttribute("team", map[i][j]->team);
			element->LinkEndChild(tile);

			if(map[i][j]->entity > 0)
				cout << map[i][j]->entity << endl;
		}
	}
	doc.LinkEndChild(element);
	doc.SaveFile("level/LV1.xml");
}